package com.spring.learn.springboot._01;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * bean初始化
 *
 * 多个bean实例调试  可以读写分离时使用
 */
@Configuration
public class Myconfig {
    @Bean(initMethod = "init",destroyMethod = "destroy")
    public BeanInitAndClose02 createBean02(){
        System.out.println("---------------");
        return  new BeanInitAndClose02();
    }

    //--------------------------------------------------开始
    //这时候我们有多个close02 那怎么找到一个呢？第一种方案是设置主方法@primary
    @Bean(initMethod = "init",destroyMethod = "destroy")
    @Primary
    public BeanInitAndClose02 createBean02Copy(){
        return  new BeanInitAndClose02();
    }
    //第二种 是注入的时候设置
    @Autowired
    @Qualifier("createBean02Copy")
    private  BeanInitAndClose02 beanInitAndClose02;
    //--------------------------------------------------结束
    @Bean
    public BeanInitAndClose01 createBean01(){
        return  new BeanInitAndClose01();
    }


}
